package com.example.springboot.controller;

import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
@PostMapping
public Map<String,Object> save(@RequestBody User user){
    Map<String,Object> res=new HashMap<>();
  if(userService.save(user) == 1){
      res.put("code",200);
      res.put("msg","成功");
  }
return res;
  //    return userService.save(user);
}
    @GetMapping
    public List<User> index(){

        return userMapper.findAll();
    }
    @DeleteMapping("/{id}")
    public  Map<String,Object> delete(@PathVariable Integer id){
Integer col=userMapper.deleteById(id);
    Map<String,Object> res=new HashMap<>();

res.put("data", col);
    return res;
    }
    //分页
    @GetMapping("/page")
    public Map<String, Object> index1(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String username){
         pageNum=(pageNum-1)*pageSize;
         List<User> data=userMapper.selectPage(pageNum,pageSize,username);
  Integer total = userMapper.selectTotal(username);
  Map<String,Object> res=new HashMap<>();
  res.put("data",data);
        res.put("total",total);

        return res;

//        return userMapper.findAll();
    }
}
